package com.koron.common.core.business.message.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.message.bean.po.MessageTemplatePo;
import com.koron.common.core.business.message.mapper.MessageTemplateMapper;
import com.koron.common.core.business.message.service.MessageTemplateService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息模板表(MessageTemplate)表服务实现类
 *
 * @author zhouj
 * @since 2022-03-16 14:13:47
 */
@Service("messageTemplateService")
public class MessageTemplateServiceImpl implements MessageTemplateService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, MessageTemplatePo messageTemplate) {

        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);

        return mapper.insertOrUpdate(messageTemplate);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<MessageTemplatePo> messageTemplateList) {

        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);

        return mapper.insertOrUpdateBatch(messageTemplateList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, MessageTemplatePo messageTemplate) {

        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);

        return mapper.insert(messageTemplate);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<MessageTemplatePo> messageTemplateList) {

        if (CollectionUtils.isEmpty(messageTemplateList)) {
            return 0;
        }

        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);
        mapper.deleteByTopicId(messageTemplateList.get(0).getTopicId());
        return mapper.insertBatch(messageTemplateList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, MessageTemplatePo messageTemplate) {
        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);
        return mapper.update(messageTemplate);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public MessageTemplatePo queryById(SessionFactory factory, String id) {

        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<MessageTemplatePo> queryList(SessionFactory factory, MessageTemplatePo messageTemplate) {

        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);

        List<MessageTemplatePo> list = mapper.queryList(messageTemplate);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, MessageTemplatePo messageTemplate, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        MessageTemplateMapper mapper = factory.getMapper(MessageTemplateMapper.class);

        List<MessageTemplatePo> list = mapper.queryList(messageTemplate);

        return new PageInfo(list);
    }

}